package com.zz.action;

import com.opensymphony.xwork2.ActionSupport;
import com.zz.model.ImportData;
import com.zz.model.Template;
import com.zz.service.ImportDataService;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: zhangzhen
 * DATE: Created in 2018/8/16 16:12
 * Description:
 */
public class ImportDataAction extends ActionSupport {

    private ImportDataService importDataService = new ImportDataService();
    private List<ImportData> importDataList = new ArrayList<ImportData>();
    private int page;
    private int rows;
    private String sort;
    private String order;

    private String templateId;
    private File fileInput;

    private String importDataId;

    public ImportDataService getImportDataService() {
        return importDataService;
    }

    public void setImportDataService(ImportDataService importDataService) {
        this.importDataService = importDataService;
    }

    public List<ImportData> getImportDataList() {
        return importDataList;
    }

    public void setImportDataList(List<ImportData> importDataList) {
        this.importDataList = importDataList;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public File getFileInput() {
        return fileInput;
    }

    public void setFileInput(File fileInput) {
        this.fileInput = fileInput;
    }

    public String getImportDataId() {
        return importDataId;
    }

    public void setImportDataId(String importDataId) {
        this.importDataId = importDataId;
    }
    // 数据列表
    public void list() {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        importDataList = importDataService.list(page, rows, sort, order);
        String json = "{\"total\":" + importDataList.size() + "," + "\"rows\":" + JSONArray.fromObject(importDataList).toString() + "}";
        try {
            response.getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void templates() {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charaset=utf-8");
        List<Template> list = new ArrayList<Template>();
        Template t = new Template();
        t.setTemplateId("student");
        t.setTemplateName("student");
        list.add(t);

        try {
            response.getWriter().write(JSONArray.fromObject(list).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
